package cn.le;

import cn.le.entty.IOTMessage;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 项目名  spring-boot
 * Created by lansp
 * Created at 2018/6/20
 * 描述:
 */
@Component
public class Producer {

	@Autowired
	private KafkaTemplate kafkaTemplate;

	public void send() throws InterruptedException {
		int i = 0;
		while (true){

			Map<String,Integer> flow = new HashMap<>();
			long time = System.currentTimeMillis();
			flow.put("161540903189368832-"+time,Integer.valueOf(String.valueOf(Math.round(Math.random()*100))));
			//kafkaTemplate.send("iot-device-flow",JSONObject.toJSONString(flow));

			IOTMessage message = new IOTMessage();
			message.setKey("161540903189368832");
			message.setTag("a");
			Map<String,Object> map = new HashMap<>();
			map.put("aiCh1",Math.random());
			map.put("aiCh2",Math.random());
			message.setPayload(map);
			String json = JSONObject.toJSONString(message);
			kafkaTemplate.send("iot-device-data",json);
			System.out.println("------------->发送成功");
			Thread.sleep(2000);
		}
	}

	public static void main(String[] args) {
		System.out.println(Math.round(Math.random()*100));
	}
}
